package org.example.testWebsocket.controller;

import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.example.testWebsocket.entity.WsMsg;
import org.example.testWebsocket.service.WebSocketService;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 测试代码！！！
 * WebSocket控制器
 * 处理客户端发送的消息
 */
@Slf4j
@RestController
public class WebSocketController {

    @Resource
    private  WebSocketService webSocketService;

    /**
     * HTTP方式发送消息（用于测试）
     */
    @RequestMapping("/api/websocket/send")
    @ResponseBody
    public String sendMessageByHttp(String username, String message) {
        WsMsg ws = new WsMsg();
        ws.setDestination(String.format("/topic/room/%s/users", username));
        ws.setMessageContent(message);
        webSocketService.sendMsg(ws);
        return "消息已发送";
    }
}

